package com.crishne.anwo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crishne.anwo.dao.RoleDao;
import com.crishne.anwo.model.Menu;
import com.crishne.anwo.model.Role;
import com.crishne.anwo.service.RoleService;
import com.crishne.anwo.utill.PaginationUtil;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	
	 @Autowired
	 private RoleDao roleDao;

	@Override
	public void addRole(Role role) {
		roleDao.addRole(role);

	}
	
	@Override
	public void updateRole(Role role){
		roleDao.updateRole(role);
	}
    
	@Override
    public void deleteRole(String stRoleId){
		roleDao.deleteRole(stRoleId);
	}
	
	@Override
	public Role selectRoleById(String id){
		return roleDao.selectRoleById(id);
		
	}
	
	@Override
	public Role selectRoleByName(String stRoleName){
		return roleDao.selectRoleByName(stRoleName);
	}

	@Override
	public List<Role> getAllRoles() {
		
		return roleDao.getAllRoles();
	}

	@SuppressWarnings("unchecked")
	@Override
	public PaginationUtil<Role> searchRoles(PaginationUtil<Role> pageConditions) {
		//return (PaginationUtil<Role>) roleDao.searchRoles(pageConditions);
		
		//当前页
        Integer currentPage = pageConditions.getCurrentPage()==null?1:pageConditions.getCurrentPage();
        //每页数据数
        Integer pageSize = pageConditions.getPageSize()==null?10:pageConditions.getPageSize();
        
    	pageConditions.setCurrentPage(currentPage);
        pageConditions.setPageSize(pageSize);
        //pageConditions.setTotalCount(userList);
        List<Role> roleList = roleDao.searchRoles(pageConditions);
        int totalCount = roleDao.getTotalResult(pageConditions);
        pageConditions.setDataList(roleList);
        pageConditions.setTotalCount(totalCount);
        if(totalCount == 0){
        	pageConditions.setTotalPage(1);
        }else if(totalCount%pageSize == 0){
        	pageConditions.setTotalPage(totalCount/pageSize);
        }else{
        	pageConditions.setTotalPage(totalCount/pageSize+1);
        }
    	return pageConditions;
	}

}
